using System;
using System.Collections.Generic;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;

namespace CMS.Data
{
    public class LinhVucDao : AbstractNHibernateDao<LinhVuc, System.Int32>, ILinhVucDao
    {
        #region Create Instance
        private static LinhVucDao instance = null;
        public static LinhVucDao Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new LinhVucDao();
                }
                return instance;
            }
        }
        #endregion     
    
        #region ILinhVucDao Members

        public byte GetMaxSTT()
        {
            byte result = 1;
            try
            {
                Object obj = null;
                String strSQL = @"Select Max(o.ThuTu)from LinhVuc o ";
                obj = NHibernateSessionManager.Instance.GetSession().CreateQuery(strSQL)
                    .UniqueResult();
                if (obj != null)
                {
                    result = Convert.ToByte(obj);
                    result += 1;
                }
                return result;
            }
            catch (Exception ex)
            {
                System.Console.Write(ex);
                return result;
            }
        }

        public LinhVuc GetLinhVucOfVietTat(string valueInput)
        {
            try
            {
                LinhVuc linhvuc = null;
                List<LinhVuc> listlinhvuc = this.GetFieldEqualCriteria("TenLinhVuc", valueInput);
                if (listlinhvuc != null && listlinhvuc.Count > 0)
                {
                    linhvuc = listlinhvuc[0];
                }
                else
                {
                    listlinhvuc = this.GetFieldEqualCriteria("VietTat", valueInput);
                    if (listlinhvuc != null && listlinhvuc.Count > 0)
                        linhvuc = listlinhvuc[0];
                }
                return linhvuc;
            }
            catch
            {
                return null;
            }
        }

        public List<LinhVuc> GetAllHasOrderBy(bool TrangThai)
        {
            try
            {
                String strQuery = @"From LinhVuc o Where o.TrangThai =:TrangThai Order by o.STT, o.TenLinhVuc";
                List<LinhVuc> Result = NHibernateSessionManager.Instance.GetSession().CreateQuery(strQuery)
                        .SetParameter("TrangThai", TrangThai)
                        .List<LinhVuc>() as List<LinhVuc>;
                return Result;
            }
            catch
            {
                return new List<LinhVuc>();
            }  
        }

        #endregion

        #region IDao<LinhVuc,byte> Members

        public LinhVuc GetById(byte id, bool shouldLock)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public LinhVuc GetById(byte id)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}
